package com.sangu.dianxiu.ui;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.R;
import com.sangu.dianxiu.adapter.RepairTypeGvAdapter;
import com.sangu.dianxiu.bean.repairtype.RepairTypeEntity;
import com.sangu.dianxiu.utils.RepairTypeManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RepairActivity extends BaseActivity {
    private RepairTypeManager manager=new RepairTypeManager();
    private RepairTypeGvAdapter gvAdapter;
    private ArrayList<RepairTypeEntity.CatesEntity> list=new ArrayList<>();
    @ViewInject(R.id.iv_activity_repair_back)       //使用xUtils注解，不用再FindViewById了
    private ImageView ivBack;
    @ViewInject(R.id.gv_activity_repair_kind)
    private GridView gvRepairKind;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair);
        ViewUtils.inject(this);
        //使用Fresco的时候要调用此方法进行初始化
        Fresco.initialize(this);
        initView();
        loadData();
    }

    private void initView() {
        gvAdapter=new RepairTypeGvAdapter(list,this);
        gvRepairKind.setAdapter(gvAdapter);
        gvRepairKind.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(RepairActivity.this,RepairProjectActivity.class);
                intent.putExtra("id",list.get(position).getId());
                startActivity(intent);
            }
        });
    }
    private void loadData() {
        HashMap<String,String> map=new HashMap<>();
        manager.getRepairType(map, new ICallBack() {
            @Override
            public void onSuccess(String string) {
                RepairTypeEntity entity= JSONObject.parseObject(string,RepairTypeEntity.class);
                if(entity.getRc()==0)
                {
                    list.addAll(entity.getCates());
                    gvAdapter.notifyDataSetChanged();
                }
            }
            @Override
            public void onError(VolleyError error) {
            }
        });
    }
    @OnClick({R.id.iv_activity_repair_back})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.iv_activity_repair_back:
                finish();
                break;
        }
    }
    //在 Activity销毁的时候取消网络请求，减少流量消耗
    @Override
    protected void onDestroy() {
        super.onDestroy();
        manager.requestQueue.cancelAll(RepairTypeManager.class);
    }
}
